<template>
  <div>
    <el-button icon="el-icon-back" circle @click="back"></el-button>
    <el-form ref="form" :model="form" status-icon :rules="rules" label-width="140px">
      <el-form-item label="仪器编号">
        <el-input v-model="form.yid" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="机构编号">
        <el-input v-model="form.jid" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="仪器说明" prop="ytext">
        <el-input v-model="form.ytext"></el-input>
      </el-form-item>
      <el-form-item label="仪器添加时间">
        <el-input v-model="form.insertdate" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="仪器状态" prop="state">
        <el-select v-model="form.state" placeholder="请选择状态类型">
          <el-option label="正常" value="正常"></el-option>
          <el-option label="损坏" value="损坏"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="upd">更新</el-button>
        <el-button @click="cancel">恢复</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "EditUser",
  yid:'',
  data(){
    return{
      temp:[],
      form: {
        yid:'',
        jid: '',
        ytext: '',
        insertdate:'',
        state:''
      },
      rules: {
        ytext: [
          {required: true, message: '鉴定机构编号不可为空', trigger: 'blur'}
        ],
        state: [
          {required: true, message: '描述信息不可为空', trigger: 'blur'}
        ]
      },
    }
  },
  created: function() {
    this.yid=this.$route.params.yid;
  },
  beforeRouteEnter: (to,from,next) => {
    console.log('进入路由之前');//加载数据
    next(vm => {
      vm.selectinfo();
    })
  },
  methods:{
    back: function (){
      this.$router.push('/func/Instrument');
    },
    selectinfo: function() {
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/instrumentAdm/selectByYid",
        params:{
          token:localStorage.getItem("token_only"),
          yid:this.yid
        }
      }).then(res=>{
        console.log(res)
        if(res["data"]["code"]==='706'){
          this.form=res["data"]["item"];
          this.temp=this.info
        }
        else if(res["data"]["code"]==='708'){
          this.$alert('该结果为空', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('身份不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    upd:function(){
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/instrumentAdm/update",
        params:{
          token:localStorage.getItem("token_only"),
          yid: this.form.yid,
          ytext: this.form.ytext,
          state:this.form.state
        }
      }).then(res=>{
        console.log(res)
        if(res["data"]["code"]==='700'){
          this.$router.push('/func/Instrument')
        }
        else if(res["data"]["code"]==='701'){
          this.$alert('系统错误！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/Instrument");
            }
          });
        }
        else if(res["data"]["code"]==='710'){
          this.$alert('仪器不存在！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/Instrument");
            }
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    cancel:function (){
        this.selectinfo();
    }
  }

}
</script>
<style scoped>

</style>
